package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.MetroCalBean;

import java.util.List;

/**
 * Created by Caleb.F
 * on 2019-12-15
 * at 23:46
 */
public class MetroResultRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<MetroCalBean> mlist;
    private Context mContext;
    private int mposition;
    private static final String TAG="ResultRecyclerAdapter";

    private static final Integer COMMON_VIEW_TYPE = 0;
    private static final Integer TRANSFOR_VIEW_TYPE = 1;

    public MetroResultRecyclerAdapter(List<MetroCalBean> list, Context Context, int position) {
        mlist = list;
        mContext = Context;
        mposition = position;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TRANSFOR_VIEW_TYPE) {
            return new MyViewholderTransFor(LayoutInflater.from(mContext).inflate(R.layout.metro_result_item_transfor, parent, false));
        } else {
            return new MyViewholderCommon(LayoutInflater.from(mContext).inflate(R.layout.metro_result_item_common, parent, false));
        }
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewholderCommon){
            ((MyViewholderCommon) holder).mTvSpotName.setText(mlist.get(mposition).getCommomList().get(holder.getAdapterPosition()));
            ((MyViewholderCommon) holder).mTvLoactionSpot.setText(String.valueOf(holder.getAdapterPosition()+1));
            ((MyViewholderCommon) holder).mImageView19.setVisibility(View.VISIBLE);
            ((MyViewholderCommon) holder).mTextView51.setVisibility(View.VISIBLE);
            if(holder.getAdapterPosition()==0){
                ((MyViewholderCommon) holder).mImageView19.setImageResource(R.mipmap.start);
                return;
            }
            //末尾位置
            else if(holder.getAdapterPosition()==mlist.get(mposition).getCommomList().size()-1){
                Log.d(TAG, "onBindViewHolder: "+holder.getAdapterPosition());
                ((MyViewholderCommon) holder).mImageView19.setImageResource(R.mipmap.end);
                ((MyViewholderCommon) holder).mTextView51.setVisibility(View.GONE);
            }
            //普通位置
            else{
                ((MyViewholderCommon) holder).mImageView19.setVisibility(View.INVISIBLE);
            }
        }
        else if (holder instanceof MyViewholderTransFor){
                ((MyViewholderTransFor) holder).mTvLoactionSpot.setText(String.valueOf(holder.getAdapterPosition()+1));
                ((MyViewholderTransFor) holder).mTvSpotName.setText(mlist.get(mposition).getCommomList().get(holder.getAdapterPosition()));
            for (MetroCalBean.TransForSite item:
                 mlist.get(mposition).getTransForSitelist()) {
                if(item.getTransforSitePosition().equals(String.valueOf(holder.getAdapterPosition()))){
                    ((MyViewholderTransFor) holder).mTvTransforInfo.setText("换乘\n"+item.getTransforLineName().substring(2));
                }
            }

        }

    }

    @Override
    public int getItemCount() {
        return Integer.parseInt(mlist.get(mposition).getAllSiteAccount());
    }

    @Override
    public int getItemViewType(int position) {
        for (MetroCalBean.TransForSite item :
                mlist.get(mposition).getTransForSitelist()) {
            if (position == Integer.parseInt(item.getTransforSitePosition())) {
                return TRANSFOR_VIEW_TYPE;
            }
        }
        return  COMMON_VIEW_TYPE;
    }

    class MyViewholderCommon extends RecyclerView.ViewHolder {
        ImageView mImageView19;
        TextView mTvLoactionSpot;
        TextView mTvSpotName;
        View mTextView51;

        public MyViewholderCommon(View convertView) {
            super(convertView);
            mImageView19 = convertView.findViewById(R.id.imageView19);
            mTvLoactionSpot = convertView.findViewById(R.id.tv_loaction_spot);
            mTvSpotName = convertView.findViewById(R.id.tv_spot_name);
            mTextView51 = convertView.findViewById(R.id.textView51);
        }
    }


    class MyViewholderTransFor extends RecyclerView.ViewHolder {
        TextView mTvTransforInfo;
        TextView mTvLoactionSpot;
        TextView mTvSpotName;
        View mTextView51;
        public MyViewholderTransFor(View convertView) {
            super(convertView);
            mTvTransforInfo = convertView.findViewById(R.id.tv_transfor_info);
            mTvLoactionSpot = convertView.findViewById(R.id.tv_loaction_spot);
            mTvSpotName = convertView.findViewById(R.id.tv_spot_name);
            mTextView51 = convertView.findViewById(R.id.textView51);

        }
    }
}
